import {
  Controller,
  Get,
  Post,
} from '../decorator/router'

import { saveQuestion, getQuestions } from '../service/questions'

@Controller('/questions')
export default class DocumentRouter {
  @Get('/:id?')
  async getQuestions (ctx, next) {
    console.log('ctx.params')
    console.log(ctx.params.id)
    console.log(ctx.query.page)

    let courseid = ctx.params.id || ''
    let page = ctx.query.page || 1
    let {data, count} = await getQuestions({courseid, page})

    ctx.body = {
      code: 0,
      data: data,
      count,
      msg: '获取问题列表成功'
    }
  }

  @Post('/')
  async submitQuestion (ctx, next) {
    const {courseid, topic, content} = ctx.request.body
    console.log(ctx.session)
    console.log(topic)
    console.log(content)
    if (!ctx.session.user) {
      return (ctx.body = {
        code: 1,
        msg: '请先登录'
      })
    }

    let question = await saveQuestion({
      email: ctx.session.user.email, 
      courseid, 
      topic,
      content
    })
    console.log(question)
    ctx.body = {
      code: 0,
      question: question,
      msg: '评论成功'
    }
  }

}
